import 'package:flutter/material.dart';
import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:window_manager/window_manager.dart';

void main() {
  runApp(const MyApp());
  // 初始化窗口设置
  doWhenWindowReady(() {
    final initialSize = Size(800, 600);
    appWindow.minSize = initialSize;
    appWindow.size = initialSize;
    appWindow.alignment = Alignment.center;
    appWindow.show();
  });
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '自定义标题栏示例',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with WindowListener {
  bool _isAlwaysOnTop = false;

  // 切换窗口置顶状态
  void _toggleAlwaysOnTop() async {
    try {
      final newValue = !_isAlwaysOnTop;
      // 先设置窗口置顶状态
      await windowManager.setAlwaysOnTop(newValue);
      // 成功后再更新UI状态
      setState(() {
        _isAlwaysOnTop = newValue;
      });
    } catch (e) {
      // 处理可能的异常
      print('设置窗口置顶状态失败: $e');
    }
  }

  @override
  void initState() {
    super.initState();
    // 确保窗口管理器初始化完成
    _initWindowManager();
  }

  // 初始化窗口管理器
  Future<void> _initWindowManager() async {
    // 必须在使用windowManager前等待初始化完成
    await windowManager.ensureInitialized();

    // 设置窗口监听器
    windowManager.addListener(this);
  }

  // 显示菜单对话框
  void _showMenu() {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('应用菜单'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: const [
            ListTile(title: Text('设置')),
            ListTile(title: Text('关于')),
            ListTile(title: Text('帮助')),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          // 主内容区域
          Expanded(
            child: Container(
              color: Colors.white,
              child: const Center(
                child: Text('应用主内容区域'),
              ),
            ),
          ),
          // 将自定义标题栏放在 Stack 顶层
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: Container(
              height: 50,
              color: Colors.blue[700],
              child: WindowTitleBarBox(
                child: Row(
                  children: [
                    // 左侧：应用名称和头像
                    Expanded(
                      child: MoveWindow(
                        child: Row(
                          children: [
                            const SizedBox(width: 12),
                            // 头像
                            const CircleAvatar(
                              radius: 16,
                              backgroundImage: AssetImage('assets/app_icon.png'),
                              child: Icon(Icons.apps, size: 18),
                            ),
                            const SizedBox(width: 8),
                            // 应用名称
                            const Text(
                              '我的应用',
                              style: TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),

                    // 右侧：自定义按钮和窗口控制按钮
                    Row(
                      children: [
                        // 菜单按钮
                        IconButton(
                          icon: const Icon(Icons.menu, color: Colors.white),
                          onPressed: _showMenu,
                          tooltip: '菜单',
                          iconSize: 18,
                          padding: const EdgeInsets.symmetric(horizontal: 12),
                          splashRadius: 20,
                        ),

                        // 置顶按钮
                        IconButton(
                          icon: Icon(
                            _isAlwaysOnTop ? Icons.push_pin : Icons.push_pin_outlined,
                            color: Colors.white,
                          ),
                          onPressed: _toggleAlwaysOnTop,
                          tooltip: _isAlwaysOnTop ? '取消置顶' : '置顶',
                          iconSize: 18,
                          padding: const EdgeInsets.symmetric(horizontal: 12),
                          splashRadius: 20,
                        ),

                        // 最小化按钮
                        MinimizeWindowButton(
                          colors: WindowButtonColors(
                            iconNormal: Colors.white,
                            mouseOver: Colors.blue[600],
                            mouseDown: Colors.blue[800],
                            iconMouseOver: Colors.white,
                            iconMouseDown: Colors.white70,
                          ),
                          onPressed: appWindow.minimize,
                        ),

                        // 最大化/还原按钮
                        MaximizeWindowButton(
                          colors: WindowButtonColors(
                            iconNormal: Colors.white,
                            mouseOver: Colors.blue[600],
                            mouseDown: Colors.blue[800],
                            iconMouseOver: Colors.white,
                            iconMouseDown: Colors.white70,
                          ),
                          onPressed: appWindow.maximizeOrRestore,
                        ),

                        // 关闭按钮
                        CloseWindowButton(
                          colors: WindowButtonColors(
                            iconNormal: Colors.white,
                            mouseOver: Colors.red,
                            mouseDown: Colors.red[700],
                            iconMouseOver: Colors.white,
                            iconMouseDown: Colors.white70,
                          ),
                          onPressed: appWindow.close,
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
